\hypertarget{heartbeat_8h}{
\section{Referência ao ficheiro heartbeat.h}
\label{heartbeat_8h}\index{heartbeat.h@{heartbeat.h}}
}
Arquivo responsável por enviar/receber mensagens de heartbeat, para detecção de possíveis falhas em servidores. 

\subsection*{Macros}
\begin{CompactItemize}
\item 
\#define \hyperlink{heartbeat_8h_bd1e24640abcadec19ebb45ce2f73c10}{HEARTBEAT\_\-TIMEOUT}~10 $\ast$ HZ
\begin{CompactList}\small\item\em Quantos segundos no maximo o \hyperlink{structcluster}{cluster} deve aguardar antes de remover um servidor do \hyperlink{structcluster}{cluster}, caso esse nao envie o heartbeat. \item\end{CompactList}\item 
\#define \hyperlink{heartbeat_8h_b573f5a033c8c8920ee38447f408bbca}{UDP\_\-HEARTBEAT\_\-PORT}~1500
\begin{CompactList}\small\item\em Porta UDP usada no envio e recebimento dos Heartbeats. \item\end{CompactList}\end{CompactItemize}
\subsection*{Funções}
\begin{CompactItemize}
\item 
void \hyperlink{heartbeat_8h_1cacc18d29de69dfe9b47ee4eb5e7600}{hb\_\-timer\_\-add} (struct \hyperlink{structcluster__node}{cluster\_\-node} $\ast$new\_\-node)
\begin{CompactList}\small\item\em Prepara o servidor adicionado ao \hyperlink{structcluster}{cluster} para ser removido do \hyperlink{structcluster}{cluster} se nao receber o heartbeat. \item\end{CompactList}\item 
void \hyperlink{heartbeat_8h_a05383ca38bdc4afb4d20da4bfe14245}{hb\_\-timer\_\-renew} (struct \hyperlink{structcluster__node}{cluster\_\-node} $\ast$node)
\begin{CompactList}\small\item\em Renova o timer do Heartbeat, caso seja recebido o heartbeat do servidor. \item\end{CompactList}\item 
int \hyperlink{heartbeat_8h_25dbc05b5c8157863bcb1801c17f77c1}{hb\_\-broadcast\_\-msg} (char $\ast$buf, int buf\_\-len)
\begin{CompactList}\small\item\em Envia uma mensagem em Brodcast para todos os nós do \hyperlink{structcluster}{cluster}. \item\end{CompactList}\item 
static void \hyperlink{heartbeat_8h_8c2bcf74f2baf343f7ee6f8cf8d26aaf}{hb\_\-rebuild\_\-msg} (void)
\begin{CompactList}\small\item\em Re-cria a mensagem de envio do heartbeat. \item\end{CompactList}\item 
static void \hyperlink{heartbeat_8h_c6c61bdfb86af95a46983e4ac5ded3e5}{nlb\_\-load\_\-heartbeat\_\-signal} (char $\ast$msg)
\begin{CompactList}\small\item\em Carrega uma mensagem de heartbear. \item\end{CompactList}\item 
static void \hyperlink{heartbeat_8h_d969b6fda1c74c864aaa7587b1d46be4}{hb\_\-udb\_\-server\_\-start} (void)
\begin{CompactList}\small\item\em Inicia o servidor UDP que irá RECEBER as mensagens de Heartbeat dos servidores do \hyperlink{structcluster}{cluster} e notificar ao \hyperlink{structcluster}{cluster} o status dessas mensagens. \item\end{CompactList}\item 
static void \hyperlink{heartbeat_8h_795b268047cc6c36f223e0fdcc5d4583}{hb\_\-udb\_\-client\_\-start} (void)
\begin{CompactList}\small\item\em Iniciar o cliente UDP que irá ENVIAR as mensagens de heartbeat deste servidor do \hyperlink{structcluster}{cluster} (\hyperlink{cluster_8h_39e3ce09327c335c6a58f152f9d8fbbf}{my\_\-cluster.my\_\-node}) A mensagem é enviada em Broadcast para o \hyperlink{structcluster}{cluster}. \item\end{CompactList}\item 
static struct \hyperlink{structksock__thread__t}{ksock\_\-thread\_\-t} $\ast$$\ast$ \hyperlink{heartbeat_8h_95053b46f210b2f1451e3314bd4e4221}{hb\_\-get\_\-udp\_\-server\_\-thread} (void)
\item 
static struct \hyperlink{structksock__thread__t}{ksock\_\-thread\_\-t} $\ast$$\ast$ \hyperlink{heartbeat_8h_61f03f1313c2bf412a60f54162b287da}{hb\_\-get\_\-udp\_\-client\_\-thread} (void)
\end{CompactItemize}
\subsection*{Variáveis}
\begin{CompactItemize}
\item 
char $\ast$ \hyperlink{heartbeat_8h_b5e2712d83aef730daa0f547e6218e9f}{hb\_\-msg\_\-buff}
\begin{CompactList}\small\item\em Mensagem enviada no Heartbeat. \item\end{CompactList}\item 
volatile int \hyperlink{heartbeat_8h_adfc50704f2f70e43acd9d50b70f9a24}{hb\_\-msg\_\-buff\_\-len} = 0
\item 
static int \hyperlink{heartbeat_8h_63e6914e51b6e79ae83f8c548c7a9e70}{broadcast} = 1
\begin{CompactList}\small\item\em Envia a mensagem em Broadcast? \item\end{CompactList}\item 
struct \hyperlink{structksock__thread__t}{ksock\_\-thread\_\-t} $\ast$ \hyperlink{heartbeat_8h_9a09c37d118a488eb9427f761be51f08}{hb\_\-udp\_\-server\_\-thread} = NULL
\begin{CompactList}\small\item\em Thread para o socket do servidor UDP. \item\end{CompactList}\item 
struct \hyperlink{structksock__thread__t}{ksock\_\-thread\_\-t} $\ast$ \hyperlink{heartbeat_8h_47b8a08bfc4c35b86ff52bce2783d51a}{hb\_\-udp\_\-client\_\-thread} = NULL
\begin{CompactList}\small\item\em Thread para o sockete do cliente UDP. \item\end{CompactList}\end{CompactItemize}


\subsection{Descrição detalhada}
Arquivo responsável por enviar/receber mensagens de heartbeat, para detecção de possíveis falhas em servidores. 

Um heartbeat é um sinal enviado por um servidor aos demais servidores para notificar que ele está operacional. 

Definido no ficheiro \hyperlink{heartbeat_8h-source}{heartbeat.h}.

\subsection{Documentação das macros}
\hypertarget{heartbeat_8h_bd1e24640abcadec19ebb45ce2f73c10}{
\index{heartbeat.h@{heartbeat.h}!HEARTBEAT\_\-TIMEOUT@{HEARTBEAT\_\-TIMEOUT}}
\index{HEARTBEAT\_\-TIMEOUT@{HEARTBEAT\_\-TIMEOUT}!heartbeat.h@{heartbeat.h}}
\subsubsection[HEARTBEAT\_\-TIMEOUT]{\setlength{\rightskip}{0pt plus 5cm}\#define HEARTBEAT\_\-TIMEOUT~10 $\ast$ HZ}}
\label{heartbeat_8h_bd1e24640abcadec19ebb45ce2f73c10}


Quantos segundos no maximo o \hyperlink{structcluster}{cluster} deve aguardar antes de remover um servidor do \hyperlink{structcluster}{cluster}, caso esse nao envie o heartbeat. 



Definido na linha 15 do ficheiro heartbeat.h.

Referenciado por hb\_\-timer\_\-add() e hb\_\-timer\_\-renew().\hypertarget{heartbeat_8h_b573f5a033c8c8920ee38447f408bbca}{
\index{heartbeat.h@{heartbeat.h}!UDP\_\-HEARTBEAT\_\-PORT@{UDP\_\-HEARTBEAT\_\-PORT}}
\index{UDP\_\-HEARTBEAT\_\-PORT@{UDP\_\-HEARTBEAT\_\-PORT}!heartbeat.h@{heartbeat.h}}
\subsubsection[UDP\_\-HEARTBEAT\_\-PORT]{\setlength{\rightskip}{0pt plus 5cm}\#define UDP\_\-HEARTBEAT\_\-PORT~1500}}
\label{heartbeat_8h_b573f5a033c8c8920ee38447f408bbca}


Porta UDP usada no envio e recebimento dos Heartbeats. 



Definido na linha 20 do ficheiro heartbeat.h.

Referenciado por hb\_\-udb\_\-client\_\-start() e hb\_\-udb\_\-server\_\-start().

\subsection{Documentação das funções}
\hypertarget{heartbeat_8h_25dbc05b5c8157863bcb1801c17f77c1}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-broadcast\_\-msg@{hb\_\-broadcast\_\-msg}}
\index{hb\_\-broadcast\_\-msg@{hb\_\-broadcast\_\-msg}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-broadcast\_\-msg]{\setlength{\rightskip}{0pt plus 5cm}int hb\_\-broadcast\_\-msg (char $\ast$ {\em buf}, \/  int {\em buf\_\-len})}}
\label{heartbeat_8h_25dbc05b5c8157863bcb1801c17f77c1}


Envia uma mensagem em Brodcast para todos os nós do \hyperlink{structcluster}{cluster}. 

\begin{Desc}
\item[Parâmetros:]
\begin{description}
\item[{\em buf}]Buffer da mensagem \item[{\em buf\_\-len}]Tamanho do buffer da mensagem \end{description}
\end{Desc}


Definido na linha 79 do ficheiro heartbeat.h.

Referências ksock\_\-thread\_\-t::addr\_\-send, ksocket\_\-send() e ksock\_\-thread\_\-t::sock.

Referenciado por hb\_\-udb\_\-client\_\-start() e remove\_\-cluster\_\-node().\hypertarget{heartbeat_8h_61f03f1313c2bf412a60f54162b287da}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-get\_\-udp\_\-client\_\-thread@{hb\_\-get\_\-udp\_\-client\_\-thread}}
\index{hb\_\-get\_\-udp\_\-client\_\-thread@{hb\_\-get\_\-udp\_\-client\_\-thread}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-get\_\-udp\_\-client\_\-thread]{\setlength{\rightskip}{0pt plus 5cm}static struct {\bf ksock\_\-thread\_\-t}$\ast$$\ast$ hb\_\-get\_\-udp\_\-client\_\-thread (void)\hspace{0.3cm}{\tt  \mbox{[}static, read\mbox{]}}}}
\label{heartbeat_8h_61f03f1313c2bf412a60f54162b287da}




Definido na linha 326 do ficheiro heartbeat.h.

Referências ksock\_\-thread\_\-t::fn e hb\_\-udb\_\-client\_\-start().

Referenciado por nlb\_\-thread\_\-init().\hypertarget{heartbeat_8h_95053b46f210b2f1451e3314bd4e4221}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-get\_\-udp\_\-server\_\-thread@{hb\_\-get\_\-udp\_\-server\_\-thread}}
\index{hb\_\-get\_\-udp\_\-server\_\-thread@{hb\_\-get\_\-udp\_\-server\_\-thread}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-get\_\-udp\_\-server\_\-thread]{\setlength{\rightskip}{0pt plus 5cm}static struct {\bf ksock\_\-thread\_\-t}$\ast$$\ast$ hb\_\-get\_\-udp\_\-server\_\-thread (void)\hspace{0.3cm}{\tt  \mbox{[}static, read\mbox{]}}}}
\label{heartbeat_8h_95053b46f210b2f1451e3314bd4e4221}




Definido na linha 316 do ficheiro heartbeat.h.

Referências ksock\_\-thread\_\-t::fn e hb\_\-udb\_\-server\_\-start().

Referenciado por nlb\_\-thread\_\-init().\hypertarget{heartbeat_8h_8c2bcf74f2baf343f7ee6f8cf8d26aaf}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-rebuild\_\-msg@{hb\_\-rebuild\_\-msg}}
\index{hb\_\-rebuild\_\-msg@{hb\_\-rebuild\_\-msg}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-rebuild\_\-msg]{\setlength{\rightskip}{0pt plus 5cm}static void hb\_\-rebuild\_\-msg (void)\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{heartbeat_8h_8c2bcf74f2baf343f7ee6f8cf8d26aaf}


Re-cria a mensagem de envio do heartbeat. 



Definido na linha 94 do ficheiro heartbeat.h.

Referências hb\_\-msg\_\-buff, hb\_\-msg\_\-buff\_\-len, cluster\_\-node::id, my\_\-cluster, cluster::my\_\-node, cluster::next\_\-serving e cluster::node\_\-count.

Referenciado por cmd\_\-my\_\-node().\hypertarget{heartbeat_8h_1cacc18d29de69dfe9b47ee4eb5e7600}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-timer\_\-add@{hb\_\-timer\_\-add}}
\index{hb\_\-timer\_\-add@{hb\_\-timer\_\-add}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-timer\_\-add]{\setlength{\rightskip}{0pt plus 5cm}void hb\_\-timer\_\-add (struct {\bf cluster\_\-node} $\ast$ {\em new\_\-node})}}
\label{heartbeat_8h_1cacc18d29de69dfe9b47ee4eb5e7600}


Prepara o servidor adicionado ao \hyperlink{structcluster}{cluster} para ser removido do \hyperlink{structcluster}{cluster} se nao receber o heartbeat. 

\begin{Desc}
\item[Parâmetros:]
\begin{description}
\item[{\em new\_\-node}]Servidor \end{description}
\end{Desc}


Definido na linha 38 do ficheiro heartbeat.h.

Referências cluster\_\-node::hb\_\-timer, HEARTBEAT\_\-TIMEOUT e remove\_\-cluster\_\-node().

Referenciado por add\_\-cluster\_\-node().\hypertarget{heartbeat_8h_a05383ca38bdc4afb4d20da4bfe14245}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-timer\_\-renew@{hb\_\-timer\_\-renew}}
\index{hb\_\-timer\_\-renew@{hb\_\-timer\_\-renew}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-timer\_\-renew]{\setlength{\rightskip}{0pt plus 5cm}void hb\_\-timer\_\-renew (struct {\bf cluster\_\-node} $\ast$ {\em node})}}
\label{heartbeat_8h_a05383ca38bdc4afb4d20da4bfe14245}


Renova o timer do Heartbeat, caso seja recebido o heartbeat do servidor. 

\begin{Desc}
\item[Parâmetros:]
\begin{description}
\item[{\em node}]Servidor \end{description}
\end{Desc}


Definido na linha 58 do ficheiro heartbeat.h.

Referências cluster\_\-node::hb\_\-timer e HEARTBEAT\_\-TIMEOUT.

Referenciado por nlb\_\-load\_\-heartbeat\_\-signal().\hypertarget{heartbeat_8h_795b268047cc6c36f223e0fdcc5d4583}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-udb\_\-client\_\-start@{hb\_\-udb\_\-client\_\-start}}
\index{hb\_\-udb\_\-client\_\-start@{hb\_\-udb\_\-client\_\-start}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-udb\_\-client\_\-start]{\setlength{\rightskip}{0pt plus 5cm}static void hb\_\-udb\_\-client\_\-start (void)\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{heartbeat_8h_795b268047cc6c36f223e0fdcc5d4583}


Iniciar o cliente UDP que irá ENVIAR as mensagens de heartbeat deste servidor do \hyperlink{structcluster}{cluster} (\hyperlink{cluster_8h_39e3ce09327c335c6a58f152f9d8fbbf}{my\_\-cluster.my\_\-node}) A mensagem é enviada em Broadcast para o \hyperlink{structcluster}{cluster}. 



Definido na linha 240 do ficheiro heartbeat.h.

Referências ksock\_\-thread\_\-t::addr, ksock\_\-thread\_\-t::addr\_\-send, broadcast, hb\_\-broadcast\_\-msg(), hb\_\-msg\_\-buff, hb\_\-msg\_\-buff\_\-len, INADDR\_\-SEND, ksock\_\-thread\_\-t::running, ksock\_\-thread\_\-t::sock, ksock\_\-thread\_\-t::thread e UDP\_\-HEARTBEAT\_\-PORT.

Referenciado por hb\_\-get\_\-udp\_\-client\_\-thread().\hypertarget{heartbeat_8h_d969b6fda1c74c864aaa7587b1d46be4}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-udb\_\-server\_\-start@{hb\_\-udb\_\-server\_\-start}}
\index{hb\_\-udb\_\-server\_\-start@{hb\_\-udb\_\-server\_\-start}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-udb\_\-server\_\-start]{\setlength{\rightskip}{0pt plus 5cm}static void hb\_\-udb\_\-server\_\-start (void)\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{heartbeat_8h_d969b6fda1c74c864aaa7587b1d46be4}


Inicia o servidor UDP que irá RECEBER as mensagens de Heartbeat dos servidores do \hyperlink{structcluster}{cluster} e notificar ao \hyperlink{structcluster}{cluster} o status dessas mensagens. 



Definido na linha 168 do ficheiro heartbeat.h.

Referências ksock\_\-thread\_\-t::addr, broadcast, ksocket\_\-receive(), nlb\_\-load\_\-heartbeat\_\-signal(), ksock\_\-thread\_\-t::running, ksock\_\-thread\_\-t::sock, ksock\_\-thread\_\-t::thread e UDP\_\-HEARTBEAT\_\-PORT.

Referenciado por hb\_\-get\_\-udp\_\-server\_\-thread().\hypertarget{heartbeat_8h_c6c61bdfb86af95a46983e4ac5ded3e5}{
\index{heartbeat.h@{heartbeat.h}!nlb\_\-load\_\-heartbeat\_\-signal@{nlb\_\-load\_\-heartbeat\_\-signal}}
\index{nlb\_\-load\_\-heartbeat\_\-signal@{nlb\_\-load\_\-heartbeat\_\-signal}!heartbeat.h@{heartbeat.h}}
\subsubsection[nlb\_\-load\_\-heartbeat\_\-signal]{\setlength{\rightskip}{0pt plus 5cm}static void nlb\_\-load\_\-heartbeat\_\-signal (char $\ast$ {\em msg})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{heartbeat_8h_c6c61bdfb86af95a46983e4ac5ded3e5}


Carrega uma mensagem de heartbear. 

\begin{Desc}
\item[Parâmetros:]
\begin{description}
\item[{\em msg}]Mensagem \end{description}
\end{Desc}


Definido na linha 121 do ficheiro heartbeat.h.

Referências get\_\-node\_\-by\_\-id(), hb\_\-timer\_\-renew() e remove\_\-cluster\_\-node().

Referenciado por hb\_\-udb\_\-server\_\-start().

\subsection{Documentação das variáveis}
\hypertarget{heartbeat_8h_63e6914e51b6e79ae83f8c548c7a9e70}{
\index{heartbeat.h@{heartbeat.h}!broadcast@{broadcast}}
\index{broadcast@{broadcast}!heartbeat.h@{heartbeat.h}}
\subsubsection[broadcast]{\setlength{\rightskip}{0pt plus 5cm}int {\bf broadcast} = 1\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{heartbeat_8h_63e6914e51b6e79ae83f8c548c7a9e70}


Envia a mensagem em Broadcast? 



Definido na linha 31 do ficheiro heartbeat.h.

Referenciado por hb\_\-udb\_\-client\_\-start() e hb\_\-udb\_\-server\_\-start().\hypertarget{heartbeat_8h_b5e2712d83aef730daa0f547e6218e9f}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-msg\_\-buff@{hb\_\-msg\_\-buff}}
\index{hb\_\-msg\_\-buff@{hb\_\-msg\_\-buff}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-msg\_\-buff]{\setlength{\rightskip}{0pt plus 5cm}char$\ast$ {\bf hb\_\-msg\_\-buff}}}
\label{heartbeat_8h_b5e2712d83aef730daa0f547e6218e9f}


Mensagem enviada no Heartbeat. 



Definido na linha 25 do ficheiro heartbeat.h.

Referenciado por hb\_\-rebuild\_\-msg() e hb\_\-udb\_\-client\_\-start().\hypertarget{heartbeat_8h_adfc50704f2f70e43acd9d50b70f9a24}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-msg\_\-buff\_\-len@{hb\_\-msg\_\-buff\_\-len}}
\index{hb\_\-msg\_\-buff\_\-len@{hb\_\-msg\_\-buff\_\-len}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-msg\_\-buff\_\-len]{\setlength{\rightskip}{0pt plus 5cm}volatile int {\bf hb\_\-msg\_\-buff\_\-len} = 0}}
\label{heartbeat_8h_adfc50704f2f70e43acd9d50b70f9a24}




Definido na linha 26 do ficheiro heartbeat.h.

Referenciado por hb\_\-rebuild\_\-msg() e hb\_\-udb\_\-client\_\-start().\hypertarget{heartbeat_8h_47b8a08bfc4c35b86ff52bce2783d51a}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-udp\_\-client\_\-thread@{hb\_\-udp\_\-client\_\-thread}}
\index{hb\_\-udp\_\-client\_\-thread@{hb\_\-udp\_\-client\_\-thread}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-udp\_\-client\_\-thread]{\setlength{\rightskip}{0pt plus 5cm}struct {\bf ksock\_\-thread\_\-t}$\ast$ {\bf hb\_\-udp\_\-client\_\-thread} = NULL}}
\label{heartbeat_8h_47b8a08bfc4c35b86ff52bce2783d51a}


Thread para o sockete do cliente UDP. 



Definido na linha 71 do ficheiro heartbeat.h.\hypertarget{heartbeat_8h_9a09c37d118a488eb9427f761be51f08}{
\index{heartbeat.h@{heartbeat.h}!hb\_\-udp\_\-server\_\-thread@{hb\_\-udp\_\-server\_\-thread}}
\index{hb\_\-udp\_\-server\_\-thread@{hb\_\-udp\_\-server\_\-thread}!heartbeat.h@{heartbeat.h}}
\subsubsection[hb\_\-udp\_\-server\_\-thread]{\setlength{\rightskip}{0pt plus 5cm}struct {\bf ksock\_\-thread\_\-t}$\ast$ {\bf hb\_\-udp\_\-server\_\-thread} = NULL}}
\label{heartbeat_8h_9a09c37d118a488eb9427f761be51f08}


Thread para o socket do servidor UDP. 



Definido na linha 66 do ficheiro heartbeat.h.

Referenciado por nlb\_\-thread\_\-exit().